package com.nursinghome.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.nursinghome.admin.service.AccountService;
import com.nursinghome.admin.service.RoomService;
import com.nursinghome.admin.service.SalaryService;
import com.nursinghome.bean.*;
import com.nursinghome.common.service.GoodsService;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/admin/file")
public class FileController {
    @Autowired
    private SalaryService SalaryService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private AccountService accountService;
    @Value("${picdir}")
    private String picdir;

    @PostMapping("/upload")
    public RespBean avatarUpload(MultipartFile pic) throws IOException {
        String filename = pic.getOriginalFilename();
        filename = UUID.randomUUID().toString().replace("-", "") + filename;
        pic.transferTo(new File(picdir + filename));
        return RespBean.ok("上传成功", filename);
    }

    //工资导出
    @GetMapping("/download")
    public void download(String data, HttpServletResponse response) throws IOException {
        if (data.length()>0 && data!=null){
            ZonedDateTime zdt = ZonedDateTime.parse(data);
            // 加上八小时
            ZonedDateTime newZdt = zdt.plusHours(8);
            // 转换为字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
            data = newZdt.format(formatter);
        }
        List<Salary> list = SalaryService.selectByPicday(data);
        ExcelUtils.download(list,"工资",response, Salary.class);
    }
    //账单的导出
    @GetMapping("/downloadacc")
    public void downloadAcc(String data, HttpServletResponse response) throws IOException {
        if (data.length()>0 && data!=null){
            ZonedDateTime zdt = ZonedDateTime.parse(data);
            // 加上八小时
            ZonedDateTime newZdt = zdt.plusHours(8);
            // 转换为字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
            data = newZdt.format(formatter);
        }
        ExcelUtils.download(accountService.selectByadata(data),"账单",response, Account.class);
    }
    //房间上传
    @PostMapping("/uploadfile")
    public RespBean ImportExcel(MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            return RespBean.error("文件夹不能为空");
        }
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        EasyExcel.read(multipartFile.getInputStream(), Room.class, new PageReadListener<Room>(dataList -> {
            for (Room room : dataList) {
                //将导入的数据用mybatis一个个添加进数据库
                try {
                    roomService.insert(room);
                } catch (NurisingHomeException e) {
                    throw new RuntimeException(e.getMessage());
                }
            }
        })).sheet().doRead();

        return RespBean.ok("上传成功");
    }
    //房间上传
    @PostMapping("/uploadGoods")
    public RespBean ImportExcelByGoods(MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            return RespBean.error("文件夹不能为空");
        }
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        EasyExcel.read(multipartFile.getInputStream(), Goods.class, new PageReadListener<Goods>(dataList -> {
            for (Goods goods : dataList) {
                //将导入的数据用mybatis一个个添加进数据库
                try {
                    goodsService.insert(goods);
                } catch (NurisingHomeException e) {
                    throw new RuntimeException(e.getMessage());
                }
            }
        })).sheet().doRead();

        return RespBean.ok("上传成功");
    }
}
